package cd.tinet.finalexam.ejb.rooms;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cl.tinet.finalexam.dao.SystemBDDAO;
import cl.tinet.finalexam.exceptions.ReserveCollitionException;
import cl.tinet.finalexam.exceptions.ReserveNotFoundException;
import cl.tinet.finalexam.exceptions.RoomFoundException;
import cl.tinet.finalexam.exceptions.RoomNotFoundException;
import cl.tinet.finalexam.exceptions.UserNotFoundException;
import cl.tinet.finalexam.model.ClassroomModel;
import cl.tinet.finalexam.model.ReserveModel;
import cl.tinet.finalexam.model.UserModel;

/**
 * 
 * Bean de sala.
 *
 * @author tinet
 */
@Stateless(mappedName = "roombeanImpl", name = "roombeanImpl")
public class RoomsBeanImpl implements RoomsBeanInterface {

    /**
     * Persistencia base de datos.
     */
    @PersistenceContext(unitName = "MysqlDS")
    private EntityManager em;

    /**
     * Dao
     */
    SystemBDDAO dataDAO;

    @PostConstruct
    public void init() {
        dataDAO = new SystemBDDAO(em);
    }

    /**
     * {@inheritDoc}
     */
    public void addRoom(ClassroomModel roomToAdd){

        dataDAO.addRoom(roomToAdd);

    }

    /**
     * {@inheritDoc}
     */
    public void removeRoom(ClassroomModel roomToRemove) throws RoomFoundException {

        if(dataDAO.searchByRoomID(roomToRemove.getRoomCode()) == null){
            dataDAO.removeRoom(roomToRemove);
        }
        else{
            throw new RoomFoundException(String.valueOf(roomToRemove.getRoomCode()));
        }

    }

}
